//
//  OptionNameListViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 16/5/30.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "OptionNameListViewController.h"

#import "UIViewExt.h"
#import "HttpRequest.h"
@interface OptionNameListViewController ()<UISearchBarDelegate>

@end
@implementation OptionNameListViewController
{
    
    NSMutableArray*codeArray;
    NSMutableArray*contentArray;
    UITableView*table;
    UILabel*noLabel;
    
}
@synthesize mDelegate;
-(void)viewDidLoad{
    
    [super viewDidLoad];
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{[super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    self.title=self.titlename;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    codeArray=[[NSMutableArray alloc]init];
    contentArray=[[NSMutableArray alloc]init];
    
        [noLabel removeFromSuperview];
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *param1 = [NSMutableDictionary new];
        [param1 setValue:Token forKey:@"tokenId"];
        [param1 setValue:self.optionName forKey:@"optionName"];
        [HttpRequest requestData:@"System/AppCodes" params:param1 success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    NSMutableArray* arrr=[[NSMutableArray alloc]init];
                    arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                    if (arrr.count==0) {
                        [noLabel removeFromSuperview];
                        noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,ScreenHeight/2-50, ScreenWidth, 17)];
                        noLabel.text=@"无数据";
                        noLabel.textColor=[UIColor blackColor];
                        noLabel.textAlignment=NSTextAlignmentCenter;
                        [self.view addSubview:noLabel];
                        
                        
                    }else{
                        for (NSDictionary* dicc in arrr )
                        {
                            [contentArray addObject:[dicc valueForKey:@"content"]];
                            [codeArray addObject:[dicc valueForKey:@"code"]];
                            
                        }
                    }
                    [table reloadData];
                    
                }else{
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];
                }
                
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
            
        }];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [contentArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = [contentArray objectAtIndex:indexPath.row] ;
    }
    if (contentArray.count !=indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(15, 55-0.35, ScreenWidth-30, 0.35)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    if (contentArray.count ==indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 55-0.35, ScreenWidth, 0.35)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.optionName isEqualToString:@"ExpressCompany"]) {
        [mDelegate ExpressCompany:contentArray[indexPath.row] ExpressCompanyCode:codeArray[indexPath.row]];
        [self.navigationController popViewControllerAnimated:YES];
    }

    if ([self.optionName isEqualToString:@"WhsInType"]) {
      
        [mDelegate WhsInType:contentArray[indexPath.row] WhsInTypeCode:codeArray[indexPath.row]];
        [self.navigationController popViewControllerAnimated:YES];
    }
    if ([self.optionName isEqualToString:@"WhsOutType"]) {
        
        [mDelegate WhsInType:contentArray[indexPath.row] WhsInTypeCode:codeArray[indexPath.row]];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

@end
